package com.css.gdzj.gdzjbxgd.modules.reportinfo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.HonestGiftChildNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.HonestGiftChildRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestGiftChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.service.HonestGiftChildService;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.viewobjects.HonestGiftChildQueryParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author z
 * @date 2020-12-11
 */
@Service
public class HonestGiftChildServiceImpl implements HonestGiftChildService {

	@Autowired
    private HonestGiftChildRepository honestGiftChildRepository;
    @Autowired
    private HonestGiftChildNativeRepository honestGiftChildNativeRepository;

    /*@Override
    public JSONObject queryHonestGiftChilds(HonestGiftChildQueryParams queryParams, int curPage, int pageSize) {
        Page<HonestGiftChildEntity> honestGiftChildPage = this.honestGiftChildNativeRepository.queryHonestGiftChilds(queryParams, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(honestGiftChildPage);
        return jsonObject;
    }*/
    @Override
    public HonestGiftChildEntity getHonestGiftChild(String id) {
        HonestGiftChildEntity honestGiftChild = this.honestGiftChildRepository.findById(id).orElse(null);
         return honestGiftChild;
    }

    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public HonestGiftChildEntity saveHonestGiftChild(HonestGiftChildEntity honestGiftChild) {
        String uuid = UUIDGenerator.getUUID();
        honestGiftChild.setUuid(uuid);
        honestGiftChild.setCreateUser(PlatformSessionUtils.getUserId());
        honestGiftChild.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.honestGiftChildRepository.save(honestGiftChild);
        return honestGiftChild;
    }*/

    /*@Override
    @Transactional(rollbackFor = Exception.class)
    public HonestGiftChildEntity updateHonestGiftChild(HonestGiftChildEntity honestGiftChild) {
        this.honestGiftChildRepository.save(honestGiftChild);
        return honestGiftChild;
    }*/

   /* @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHonestGiftChilds(List<String> ids) {
        List<HonestGiftChildEntity> honestGiftChildList = this.honestGiftChildRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(honestGiftChildList) && honestGiftChildList.size() > 0) {
            for(HonestGiftChildEntity honestGiftChild : honestGiftChildList) {
                this.honestGiftChildRepository.delete(honestGiftChild);
            }
        }
    }*/
    /*@Override
    public void exportExcel(HonestGiftChildQueryParams queryParams, HttpServletResponse response){
    List<HonestGiftChildEntity> honestGiftChildList = honestGiftChildNativeRepository.queryHonestGiftChild(queryParams);
    List<HonestGiftChildExportExcelVO> excelVos = new ArrayList<>();

    honestGiftChildList.forEach(e->{
    HonestGiftChildExportExcelVO honestGiftChildExcelVo = new HonestGiftChildExportExcelVO();
    BeanUtils.copyProperties(e,honestGiftChildExcelVo);
    excelVos.add(honestGiftChildExcelVo);
    });
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "honest_gift_child"), HonestGiftChildExportExcelVO.class, excelVos);
    PlatformDownloadUtils.exportExcel(workbook, "honest_gift_child.xls", response);

    }
    @Override
    public String uploadFile(MultipartFile file, HttpServletResponse response) {
    try{
    StringBuilder returnMsg = new StringBuilder();
    ImportParams params = new ImportParams();
    params.setHeadRows(1);
    params.setNeedVerify(true);

    ExcelImportResult<HonestGiftChildExcelVO> result = ExcelImportUtil.importExcelMore(file.getInputStream(),HonestGiftChildExcelVO.class,params);
    List<HonestGiftChildExcelVO> honestGiftChildExcelVoList = result.getFailList();
    for(int i = 0;i<honestGiftChildExcelVoList.size();i++){
        HonestGiftChildExcelVO honestGiftChildExcelVo = honestGiftChildExcelVoList.get(i);
        String msg = honestGiftChildExcelVo.getErrorMsg();
        Integer num = honestGiftChildExcelVo.getRowNum();
        if(PlatformObjectUtils.checkFieldAllNull(honestGiftChildExcelVo)){
        honestGiftChildExcelVoList.remove(i);
        i--;
    }else{
        honestGiftChildExcelVo.setRowNum(num);
        honestGiftChildExcelVo.setErrorMsg(msg);
            }
    }

    List<HonestGiftChildEntity>  dataEntityList = new ArrayList<>();
    List<HonestGiftChildExcelVO> list = result.getList();
            list.forEach(e->{
            HonestGiftChildEntity dataEntity = new HonestGiftChildEntity();
            BeanUtils.copyProperties(e,dataEntity);
            dataEntity.setId(UUIDGenerator.getUUID());
            dataEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
            dataEntity.setCreateUser(PlatformSessionUtils.getUserId());
            dataEntityList.add(dataEntity);
            });
    honestGiftChildRepository.saveAll(dataEntityList);
    int successListSize = list.size() - honestGiftChildExcelVoList.size();
    returnMsg.append("成功导入" + successListSize + "条，失败" + honestGiftChildExcelVoList + "条");

    for (HonestGiftChildExcelVO entity : result.getFailList()) {
        String msg = "";
        returnMsg.append("第" + entity.getRowNum() + "行" + entity.getErrorMsg() + "<br/>");
        returnMsg.append(msg);
    }
            return returnMsg.toString();
    }catch (Exception e){
            e.printStackTrace();
    return "导入失败，请检查数据正确性";

    }
   }*/

    @Override
    public List<HonestGiftChildEntity> getList(String bxgdReportInfoId) {
        List<HonestGiftChildEntity>honestGiftChildEntityList =  this.honestGiftChildNativeRepository.getList(bxgdReportInfoId);
        return honestGiftChildEntityList;
    }

}
